package org.epanetgrid.otimizacao.evaluators;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.epanetgrid.otimizacao.searchspace.BulkIndividualEvaluator;
import org.jheuristics.Individual;
import org.jheuristics.ga.FitnessEvaluator;
import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.Population;
import org.jheuristics.ga.SimpleBulkFitnessEvaluator;


public class BulkFitnessBulkIndividualEvaluator extends SimpleBulkFitnessEvaluator {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4781297473873771943L;
	
	private BulkIndividualEvaluator bulkIndividualEvaluator;

	/**
	 * 
	 * @param fitnessEvaluator
	 * @param bulkIndividualEvaluator
	 */
	public BulkFitnessBulkIndividualEvaluator(FitnessEvaluator fitnessEvaluator, BulkIndividualEvaluator bulkIndividualEvaluator) {
		super(fitnessEvaluator);
		this.bulkIndividualEvaluator = bulkIndividualEvaluator;
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.BulkFitnessEvaluator#evaluateFitness(org.jheuristics.ga.Population, org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
	 */
	public void evaluateFitness(Population population, GAStatus status, GAConfig config) {
		List oldIndividuals = Arrays.asList(population.toIndividualArray());
		List individuals = bulkIndividualEvaluator.evaluate(oldIndividuals);
		for (Iterator it = individuals.iterator(), oldIt = oldIndividuals.iterator(); it.hasNext() && oldIt.hasNext(); ) {
			Individual individual = (Individual) it.next();
			Individual oldIndividual = (Individual) oldIt.next();
			oldIndividual.setDataApplication(individual.getDataApplication());
		}
		super.evaluateFitness(population, status, config);
	}

}
